package com.example.innovate.controller;

/*
    2021.9.13 update
*/

import com.example.innovate.bean.Result;
import com.example.innovate.domain.*;
import com.example.innovate.service.*;
import com.example.innovate.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/api/user")
@Api(tags = "用户接口")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    AchieveCompetitionService achieveCompetitionService;
    @Autowired
    CompetitionRefuseService competitionRefuseService;
    @Autowired
    CreatedClassService createdClassService;
    @Autowired
    StudentService studentService;
    @Autowired
    GraduateStudentService graduateStudentService;


    //@UserLoginToken
    @GetMapping("/findCreatedClass")
    @ApiOperation("查找双创班级")
    public Result findAllClass(@RequestParam("pageNumber") Integer pageNumber,
                               @RequestParam("pageSize") Integer pageSize,
                               @RequestParam("className") String className) throws Exception{
        if (pageNumber == null){
            pageNumber = 1;
        }
        List<CreatedClassDO> list = createdClassService.findAllByClassName(className,pageNumber,pageSize);
        return new Result(200,"success","返回成功",list);
    }


    //@UserLoginToken
    @GetMapping("/findCompetition")
    @ApiOperation("查找竞赛信息")
    public Result findCompetition(@RequestParam("userId") int id,
                                  @RequestParam("pageNumber") Integer pageNumber,
                                  @RequestParam("pageSize") Integer pageSize) throws Exception{
        List<AchievementCompetitionDO> list = achieveCompetitionService.findByUserId(id,pageNumber,pageSize);
        return new Result(200,"success","返回成功",list);
    }

    //@UserLoginToken
    @GetMapping("/findStudent")
    @ApiOperation("查找学生信息")
    public Result findStudent(@RequestParam("userId") int id,
                              @RequestParam("pageNumber") Integer pageNumber,
                              @RequestParam("pageSize") Integer pageSize) throws Exception{
        List<StudentInfoDO> studentInfoDO = studentService.findByUserId(id,pageNumber,pageSize);
        return new Result(200,"success","返回成功",studentInfoDO);
    }

    //@UserLoginToken
    @GetMapping("/findGraStudent")
    @ApiOperation("查找毕业生信息")
    public Result findGraStudent(@RequestParam("userId") int id,
                                 @RequestParam("pageNumber") Integer pageNumber,
                                 @RequestParam("pageSize") Integer pageSize) throws Exception{
        List<GraduateInfoDO> graduateInfoDO = graduateStudentService.findByUserId(id,pageNumber,pageSize);
        return new Result(200,"success","返回成功",graduateInfoDO);
    }

    //@UserLoginToken
    @GetMapping("/findExCompetition")
    @ApiOperation("查看审核中竞赛信息")
    public Result findCompetitionByUserId(@RequestParam("userId") int id,
                                          @RequestParam("pageNumber") Integer pageNumber,
                                          @RequestParam("pageSize") Integer pageSize) throws Exception{
        List<AchievementCompetitionDO> list = achieveCompetitionService.findAllExamineByUserId(id,pageNumber,pageSize);
        return new Result(200,"success","返回成功",list);
    }


    //@UserLoginToken
    @GetMapping("/updateCompetition")
    @ApiOperation("修改审核中竞赛信息")
    public Result updateCompetition(@RequestBody AchievementCompetitionDO achievementCompetitionDO,
                                    @RequestParam("userId") int id) throws Exception{
        if(id==achievementCompetitionDO.getComUserId()){
            achieveCompetitionService.updateCompetiton(achievementCompetitionDO);
            return new Result(200,"success","修改成功",achievementCompetitionDO);
        }
        else{
            return new Result(400,"error","只能修改本人信息",null);
        }

    }

    //@UserLoginToken
    @PostMapping("/upload")
    @ApiOperation("修改审核中文件")
    public Result upload(@RequestParam("file") MultipartFile file, @RequestParam("id") int id) throws Exception{
        AchievementCompetitionDO achievementCompetitionDO = achieveCompetitionService.findCompetitionById(id);
        String fileName = FileUtil.upload(file);
        if(achievementCompetitionDO.getComRemark()!=null){
            //删除原有文件
            String deleteName = achievementCompetitionDO.getComRemark();
            String filePath = "/root/file/" ;//C:\\file\\
            File filePast = new File(filePath + deleteName);
            if (filePast.isFile() && filePast.exists()) {
                filePast.delete();
            }
        }
        //加入新文件
        achievementCompetitionDO.setComRemark(fileName);
        achieveCompetitionService.updateCompetiton(achievementCompetitionDO);
        return new Result(200,"success","添加成功",achievementCompetitionDO);
    }

    //@UserLoginToken
    @GetMapping("/deleteCompetition")
    @ApiOperation("删除审核中竞赛信息")
    public Result deleteCompetition(@RequestParam("id") int id,
                                    @RequestParam("userId") int userId) throws Exception{
        AchievementCompetitionDO achievementCompetitionDO = achieveCompetitionService.findCompetitionById(id);
        if(achievementCompetitionDO!=null){
            if(id==achievementCompetitionDO.getComUserId()){
                achieveCompetitionService.deleteCompetition(id);
                return new Result(200,"success","删除成功","id="+id);
            }
            else{
                return new Result(400,"error","只能删除本人信息",null);
            }
        }
        else{
            return new Result(400,"error","所删除信息不存在",null);
        }

    }

    //@UserLoginToken
    @GetMapping("/findAllRefuse")
    @ApiOperation("查看学科竞赛被拒绝理由")
    public Result findAllRefuse(@RequestParam("userId") int id) throws Exception{
        List<CompetitionRefuseDO> list = competitionRefuseService.findByUserId(id);
        return new Result(200,"success","返回成功",list);
    }





}
